package smartgrid;

import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import org.hibernate.criterion.Restrictions;
import org.orm.PersistentException;
import smartgrid.exceptions.CantRemoveFromDBexception;
import smartgrid.exceptions.ContractNotExistsException;
import smartgrid.exceptions.FacebookAccountDisabledException;

@Stateless
public class AnnotationManager implements AnnotationInterface {

    @Override
    public List<Annotation> getFacebookAccountAnnotations(String facebookUID) throws FacebookAccountDisabledException {
        try {
            FaceBookAccountCriteria crit = new FaceBookAccountCriteria();
            crit.add(Restrictions.eq("UId", facebookUID));
            FaceBookAccount fb = FaceBookAccount.loadFaceBookAccountByCriteria(crit);

            if (fb.getEnable() == false) {
                throw new FacebookAccountDisabledException();
            }

            AnnotationCriteria aCrit = new AnnotationCriteria();
            aCrit.add(Restrictions.eq("faceBookAccount.id", fb.getID()));
            Annotation[] listAnnotationByCriteria = Annotation.listAnnotationByCriteria(aCrit);

            return (Arrays.asList(listAnnotationByCriteria));

        } catch (PersistentException ex) {
            Logger.getLogger(AnnotationManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public Annotation insertAnnotation(String FacebookUID, String contractNumber, String annotationName, GregorianCalendar calendar) throws FacebookAccountDisabledException, ContractNotExistsException {
        FaceBookAccountCriteria fcrit;
        try {
            fcrit = new FaceBookAccountCriteria();
            fcrit.add(Restrictions.eq("UId", FacebookUID));
            FaceBookAccount fb = FaceBookAccount.loadFaceBookAccountByCriteria(fcrit);

            if (fb.getEnable() == false) {
                throw new FacebookAccountDisabledException();
            }

            smartgrid.ContractCriteria crit = new ContractCriteria();
            crit.add(Restrictions.eq("Number", contractNumber));
            smartgrid.Contract contract = smartgrid.Contract.loadContractByCriteria(crit);

            if (contract == null) {
                throw new ContractNotExistsException();
            }

            Annotation a = Annotation.createAnnotation();
            a.setContract(contract);
            a.setDate(ETL.ETL.gCalenderToSqlDate(calendar));
            a.setFaceBookAccount(fb);
            a.setName(annotationName);
            a.save();

            return a;
        } catch (PersistentException ex) {
            Logger.getLogger(AnnotationManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public List<Annotation> getContractFacebookAccountAnnotations(String facebookUID, String contractNumber) throws FacebookAccountDisabledException, ContractNotExistsException {
        FaceBookAccountCriteria fcrit;
        try {
            fcrit = new FaceBookAccountCriteria();
            fcrit.add(Restrictions.eq("UId", facebookUID));
            FaceBookAccount fb = FaceBookAccount.loadFaceBookAccountByCriteria(fcrit);

            if (fb.getEnable() == false) {
                throw new FacebookAccountDisabledException();
            }

            smartgrid.ContractCriteria crit = new ContractCriteria();
            crit.add(Restrictions.eq("Number", contractNumber));
            smartgrid.Contract contract = smartgrid.Contract.loadContractByCriteria(crit);

            if (contract == null) {
                throw new ContractNotExistsException();
            }

            AnnotationCriteria aCrit = new AnnotationCriteria();
            aCrit.add(Restrictions.eq("faceBookAccount.id", fb.getID()));
            aCrit.add(Restrictions.eq("contract.id", contract.getID()));
            Annotation[] listAnnotationByCriteria = Annotation.listAnnotationByCriteria(aCrit);

            return (Arrays.asList(listAnnotationByCriteria));
        } catch (PersistentException ex) {
            Logger.getLogger(AnnotationManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public void removeAnnotation(String facebookUID, String contractNumber, Integer annotationID) throws FacebookAccountDisabledException, ContractNotExistsException, CantRemoveFromDBexception {
        FaceBookAccountCriteria fcrit;
        try {
            fcrit = new FaceBookAccountCriteria();
            fcrit.add(Restrictions.eq("UId", facebookUID));
            FaceBookAccount fb = FaceBookAccount.loadFaceBookAccountByCriteria(fcrit);

            if (fb.getEnable() == false) {
                throw new FacebookAccountDisabledException();
            }

            smartgrid.ContractCriteria crit = new ContractCriteria();
            crit.add(Restrictions.eq("Number", contractNumber));
            smartgrid.Contract contract = smartgrid.Contract.loadContractByCriteria(crit);

            if (contract == null) {
                throw new ContractNotExistsException();
            }
            
            Annotation a = Annotation.getAnnotationByORMID(annotationID);
            a.deleteAndDissociate();
        } catch (PersistentException ex) {
            Logger.getLogger(AnnotationManager.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

